package cn.terys.terystransactionclient.data;

import java.util.Hashtable;
import java.util.Map;

import android.content.Context;
import cn.terys.terystransactionclient.R;

public class CodeException extends Exception
{
    /**
     * 
     */
    private static final long          serialVersionUID               = 1L;

    public static final String         UNKNOWNERROR_CODE              = "-1000";
    public static final String         NETERROR_CODE                  = "-1";
    public static final String         NETERROR_TRANS_OPERATE_CODE    = "-2";
    public static final String         NETERROR_TRANS_QUERY_CODE      = "-3";
    public static final String         TOOLONG_CODE                   = "-4";
    public static final String         XMLERROR_CODE                  = "-5";
    public static final String         MD5ERROR_CODE                  = "-6";
    public static final String         QRCODE_CREATEERROR_CODE        = "-7";
    public static final String         DISKERROR_CODE                 = "-8";
    public static final String         SIGNOFF_EXISTUNKNOWNTRANS_CODE = "-9";

    private static Map<String, String> CODE_MSGS                      = new Hashtable<String, String>();

    private String                     code                           = null;

    public static void initCodeMsgs(Context context)
    {
        CODE_MSGS.put("00", context.getString(R.string.generic_code_00));
        CODE_MSGS.put("01", context.getString(R.string.generic_code_01));
        CODE_MSGS.put("51", context.getString(R.string.generic_code_51));
        CODE_MSGS.put("52", context.getString(R.string.generic_code_52));
        CODE_MSGS.put("53", context.getString(R.string.generic_code_53));
        CODE_MSGS.put("96", context.getString(R.string.generic_code_96));
        CODE_MSGS.put("99", context.getString(R.string.generic_code_99));
        CODE_MSGS.put("80", context.getString(R.string.generic_code_80));
        CODE_MSGS.put("61", context.getString(R.string.generic_code_61));
        CODE_MSGS.put("81", context.getString(R.string.generic_code_81));
        CODE_MSGS.put("82", context.getString(R.string.generic_code_82));
        CODE_MSGS.put("71", context.getString(R.string.generic_code_71));
        CODE_MSGS.put("97", context.getString(R.string.generic_code_97));
        CODE_MSGS.put("95", context.getString(R.string.generic_code_95));
        CODE_MSGS.put("94", context.getString(R.string.generic_code_94));
        CODE_MSGS.put("93", context.getString(R.string.generic_code_93));
        CODE_MSGS.put("92", context.getString(R.string.generic_code_92));
        CODE_MSGS.put("91", context.getString(R.string.generic_code_91));
        CODE_MSGS.put("30", context.getString(R.string.generic_code_30));
        CODE_MSGS.put("31", context.getString(R.string.generic_code_31));
        CODE_MSGS.put("72", context.getString(R.string.generic_code_72));
        CODE_MSGS.put("73", context.getString(R.string.generic_code_73));
        CODE_MSGS.put("74", context.getString(R.string.generic_code_74));
        CODE_MSGS.put("75", context.getString(R.string.generic_code_75));
        CODE_MSGS.put("76", context.getString(R.string.generic_code_76));
        CODE_MSGS.put("49", context.getString(R.string.generic_code_49));
        CODE_MSGS.put("60", context.getString(R.string.generic_code_60));
        CODE_MSGS.put("62", context.getString(R.string.generic_code_62));
        CODE_MSGS.put("64", context.getString(R.string.generic_code_64));
        CODE_MSGS.put("65", context.getString(R.string.generic_code_65));
        CODE_MSGS.put("48", context.getString(R.string.generic_code_48));
        CODE_MSGS.put("98", context.getString(R.string.generic_code_98));
        CODE_MSGS.put("35", context.getString(R.string.generic_code_35));
        CODE_MSGS.put("36", context.getString(R.string.generic_code_36));
        CODE_MSGS.put("37", context.getString(R.string.generic_code_37));
        CODE_MSGS.put("38", context.getString(R.string.generic_code_38));
        CODE_MSGS.put(UNKNOWNERROR_CODE, context.getString(R.string.generic_unknownerror));
        CODE_MSGS.put(NETERROR_CODE, context.getString(R.string.generic_code_local_neterror));
        CODE_MSGS.put(NETERROR_TRANS_OPERATE_CODE, context.getString(R.string.generic_code_local_neterror_trans_operate));
        CODE_MSGS.put(NETERROR_TRANS_QUERY_CODE, context.getString(R.string.generic_code_local_neterror_trans_query));
        CODE_MSGS.put(TOOLONG_CODE, context.getString(R.string.generic_code_local_toolong));
        CODE_MSGS.put(XMLERROR_CODE, context.getString(R.string.generic_code_local_xmlerror));
        CODE_MSGS.put(MD5ERROR_CODE, context.getString(R.string.generic_code_local_md5error));
        CODE_MSGS.put(QRCODE_CREATEERROR_CODE, context.getString(R.string.generic_code_local_qrcode_createerror));
        CODE_MSGS.put(DISKERROR_CODE, context.getString(R.string.generic_code_local_diskerror));
        CODE_MSGS.put(SIGNOFF_EXISTUNKNOWNTRANS_CODE, context.getString(R.string.generic_code_local_signoff_existunknowntrans));
    }

    private static String getCodeMsg(Context context, String code)
    {
        String msg = CODE_MSGS.get(code);
        if (msg == null)
            msg = context.getString(R.string.generic_unknownerror);
        return msg + "(code:" + code + ")";
    }

    public CodeException(Context context, String code)
    {
        super(getCodeMsg(context, code));
        this.code = code;
    }

    public CodeException(Context context, String code, Throwable throwable)
    {
        super(getCodeMsg(context, code), throwable);
        this.code = code;
    }

    public String getCode()
    {
        return code;
    }

}
